package org.framework.define.error;

import java.util.Locale;
import java.util.Properties;

import org.framework.core.Configation;
import org.framework.core.error.LanguageErrcode;


public enum CustomErrors implements LanguageErrcode {
	/*
	 * 用户错误集,result=3
	 */
	/** 用户不存在或密码错误 */
	USER_ERROR_TOKEN(3, "用户未登录或失效"),
	USER_ERROR_ACCOUNT(3, "用户不存在或密码错误"),
	USER_ERROR_EXIST(3, "用户已存在"),
	USER_ERROR_NOT_EXIST(3, "用户不存在"),
	USER_ERROR_PASSWORD(3, "原始密码错误"),
	USER_ERROR_OPER_PASSWORD(3, "原始操作密码错误"),
	USER_ACCOUNT_ERROR_BALANCE_NOT_ENOUGH(3, "账户余额不足"),
	USER_ACCOUNT_ERROR_COINS_NOT_ENOUGH(3, "账户金币不足"),
	USER_ACCOUNT_ERROR_CREDIT_NOT_ENOUGH(3, "账户积分不足"),
	
	USER_BLACKLIST_ERROR_ADDSELF(3, "不能将自己添加为黑名单"),
	USER_BLACKLIST_ERROR_NOT_EXIST(3, "黑名单不存在"),
	USER_BLACKLIST_ERROR_EXIST(3, "黑名单已存在"),
	
	USER_BIZACCOUNT_ERROR_NOT_EXIST_IN_WEBSITE(3, "该账号不存在或被封"),
	USER_BIZACCOUNT_ERROR_EXIST(3, "交易账号已存在"),
	USER_BIZACCOUNT_ERROR_NOT_EXIST(3, "交易账号不存在"),
	USER_BIZACCOUNT_ERROR_NOT_REAL(3, "交易账号未实名"),
	USER_BIZACCOUNT_ERROR_EXCEED(3, "交易账号个数超过限制"),
	/*
	 * 任务错误集,result=4
	 */
	TASK_ERROR_PRODUCT(4, "宝贝信息有误"),
	TASK_ERROR_ACCOUNT_NOT_MATCH(4, "宝贝与掌柜不匹配"),
	TASK_ERROR_RESET(4, "任务不可重置"),
	TASK_ERROR_TAKE_EXIST(4, "不能重复接取任务"),
	TASK_ERROR_TAKE_IP(4, "不能接手相同IP地址任务"),
	TASK_ERROR_TAKE_IP_LIMIT(4, "当前IP任务数超过上限"),
	TASK_ERROR_TAKE_LIMIT(4, "同时进行任务数超过上限"),
	TASK_ERROR_TAKE_SELF(4, "不能接手自己发布的任务"),
	TASK_ERROR_TAKE_PROVINCE(4, "任务指定接手省份不符"),
	TASK_ERROR_TAKE_CITY(4, "任务指定接手城市不符"),
	TASK_ERROR_TAKE_PRODUCT_REPEAT(4, "15天内不能接手相同宝贝"),
	TASK_ERROR_TAKE_TYPE_REPEAT(4, "同一掌柜一个月内只能接手一次套餐或购物车任务"),
	TASK_ERROR_TAKE_SELLER_LIMIT(4, "同一掌柜一个月内接手任务数量不能超过{0}个"),
	TASK_ERROR_NOT_EXIST(4, "任务不存在"),
	TASK_ERROR_EXIT(4, "任务不可退出");
	
	public Locale defaultLanguage;	//默认语言
	{
		String[] language = Configation.getSysProp("sys.defaultLanguage").split("_");
		if(language != null && language.length > 1){
			defaultLanguage = new Locale(language[0],language[1]);
		}else{
			defaultLanguage = Locale.getDefault();
		}
	}
	private final int result;		//一级错误码
	private final int errcode;		//二级错误码
	private final String msg;		//错误描述
	
	private CustomErrors(int result) {
		this.result = result;
		this.errcode = this.ordinal();
		this.msg = matchMsg(name(), defaultLanguage);
	}
	
	private CustomErrors(int result, String msg) {
		this.result = result;
		this.errcode = this.ordinal();
		this.msg = msg;
	}
	
	private CustomErrors(int result, int errcode) {
		this.result = result;
		this.errcode = errcode;
		this.msg = matchMsg(name(), defaultLanguage);
	}
	
	private CustomErrors(int result, int errcode, String msg) {
		this.result = result;
		this.errcode = errcode;
		this.msg = msg;
	}
	
	public int getResult() {
		return this.result;
	}
	public int getErrcode() {
		return this.errcode;
	}
	public String getMsg() {
		return this.msg;
	}
	public String getMsg(Locale locale){
		return matchMsg(name(), locale);
	}
	
	public LanguageErrcode LOCALE(final Locale locale){
		return new LanguageErrcode() {
			
			public int getResult() {
				return result;
			}
			
			public String getMsg() {
				return matchMsg(name(), locale);
			}
			
			public int getErrcode() {
				return errcode;
			}

			public LanguageErrcode LOCALE(Locale locale) {
				return CustomErrors.this.LOCALE(locale);
			}
		};
	}
	
	private static String matchMsg(String key, String language){
		Properties p = Configation.getProp(language);
		if(p==null)
			return null;
		
		String msg = p.getProperty(key);
		
		return msg==null?("Error:["+key+"]"):msg;
	}
	
	public static String matchMsg(String key, Locale locale){
		return matchMsg(key, locale.toString());
	}
}
